package com.corpgovernment.organization.controller;

import com.corpgovernment.api.organization.model.ffpinfo.UserFfpVo;
import com.corpgovernment.api.organization.model.idcard.UserIdCardVo;
import com.corpgovernment.api.organization.model.user.employee.UserBasicInfo;
import com.corpgovernment.api.organization.model.user.employee.UserIndexInfoVo;
import com.corpgovernment.common.base.BaseUserInfo;
import com.corpgovernment.common.base.JSONResult;
import com.corpgovernment.common.common.CorpBusinessException;
import com.corpgovernment.common.enums.ExceptionCodeEnum;
import com.corpgovernment.organization.constant.OrganizationResponseCodeEnum;
import com.ctrip.corp.obt.generic.utils.StringUtils;
import com.corpgovernment.organization.entity.db.MbOrgUserIdcard;
import com.corpgovernment.organization.service.IOrgUserIdCardService;
import com.corpgovernment.organization.service.IOrganizationUserFfpService;
import com.corpgovernment.organization.service.UserIndexInfoService;
import com.ctrip.corp.obt.generic.core.context.UserInfoContext;
import com.ctrip.corp.obt.generic.utils.CollectionUtils;
import com.ctrip.corp.obt.generic.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;


@RequestMapping(value = "/userIndexInfoController")
@RestController
public class UserIndexInfoController {
    @Autowired
    private UserIndexInfoService userIndexInfoService;
    @Autowired
    private IOrgUserIdCardService organizationUserIdCardService;
    @Autowired
    private IOrganizationUserFfpService organizationUserFfpService;

    /**
     * 查询用户基础信息证件信息和常旅客信息
     *
     * @param baseUserInfo
     * @return
     */
    @RequestMapping(value = "/getUserInxeInfo")
    public JSONResult<UserIndexInfoVo> getUserInxeInfo(BaseUserInfo baseUserInfo) {
        //uId = "145@1234.CSZGS";
        return userIndexInfoService.getIndexIfo(baseUserInfo);
    }

    /**
     * 更新用户信息
     *
     * @param userBasicInfo
     * @return
     */
    @RequestMapping(value = "/updateUserBasicInfo")
    public JSONResult updateUserBasicInfo(@RequestBody UserBasicInfo userBasicInfo) {
        return userIndexInfoService.updateUserBasicInfo(userBasicInfo);
    }

    /**
     * 查询用户证件信息
     *
     * @param uId
     * @return
     */
    @RequestMapping(value = "/findUserCardByEmployeeUid")
    public JSONResult<List<MbOrgUserIdcard>> findUserCardByEmployeeUid(String uId) {
        //uId = "145@1234.CSZGS";
        if (StringUtils.isBlank(uId)) {
            uId = Objects.nonNull(UserInfoContext.getContextParams("uid")) ? UserInfoContext.getContextParams("uid").toString() : "";
        }
        return userIndexInfoService.findUserCardByEmployeeUid(uId);
    }


    /**
     * 检查用户证件信息，没有保存，有则更新
     *
     * @param
     * @return
     */
    @RequestMapping("/check")
    public JSONResult check(@RequestBody List<UserIdCardVo> userIdcards, String uId) {
        if (StringUtils.isBlank(uId)){
            BaseUserInfo baseUserInfo = UserInfoContext.getContextParams(BaseUserInfo.class);
            uId = Optional.ofNullable(baseUserInfo).map(BaseUserInfo::getUid).orElseThrow(() -> new CorpBusinessException(OrganizationResponseCodeEnum.UID_IS_NULL));
        }
        for (UserIdCardVo userIdCardVo : userIdcards) {
            Integer id = userIdCardVo.getId();
            // 保存流程
            if (id == null) {
                //验证证件
                boolean valid = organizationUserIdCardService.validateCardInfo(userIdCardVo);
                if(!valid) {
                    continue;
                }
                MbOrgUserIdcard mbOrgUserIdcard = new MbOrgUserIdcard();
                Integer cardType = userIdCardVo.getCardType();
                String cardNo = userIdCardVo.getCardNo();
                mbOrgUserIdcard.setCardType(cardType);
                mbOrgUserIdcard.setCardNo(cardNo);
                mbOrgUserIdcard.setTimeLimit(userIdCardVo.getTimeLimit());
                mbOrgUserIdcard.setEmployeeUid(uId);
                mbOrgUserIdcard.setPlaceOfIssue(userIdCardVo.getPlaceOfIssue());
                mbOrgUserIdcard.setDeleteTime(new Date(0));
                List<MbOrgUserIdcard> cardList = organizationUserIdCardService.findByTypeAndNo(cardType, cardNo);
                if (!CollectionUtils.isEmpty(cardList)) {
                    throw new CorpBusinessException(OrganizationResponseCodeEnum.CARD_EXISTS);
                }
                organizationUserIdCardService.create(mbOrgUserIdcard);
            }
            // 修改流程 
            if (id != null) {
                //验证证件
                boolean valid = organizationUserIdCardService.validateCardInfo(userIdCardVo);
                if(!valid) {
                    continue;
                }
                MbOrgUserIdcard mbOrgUserIdcard = new MbOrgUserIdcard();
                mbOrgUserIdcard.setCardType(userIdCardVo.getCardType());
                mbOrgUserIdcard.setCardNo(userIdCardVo.getCardNo());
                mbOrgUserIdcard.setTimeLimit(userIdCardVo.getTimeLimit());
                mbOrgUserIdcard.setPlaceOfIssue(userIdCardVo.getPlaceOfIssue());
                mbOrgUserIdcard.setId(Long.valueOf(userIdCardVo.getId()));
                organizationUserIdCardService.update(mbOrgUserIdcard);
            }

        }
        return JSONResult.ok();
    }

    /**
     * 删除用户证件信息
     *
     * @return
     */
    @RequestMapping("/deleteUserIdCard")
    public JSONResult deleteUserIdCard(@RequestBody Map para) {
        boolean flag = organizationUserIdCardService.delete((Integer) para.get("id"));
        return JSONResult.success(flag);
    }

    /**
     * 查询用户证件信息
     *
     * @return
     */
    @RequestMapping(value = "/findUserFfpByEmployeeUid")
    public JSONResult<List<UserFfpVo>> findUserFfpByEmployeeUid() {
        BaseUserInfo baseUserInfo = UserInfoContext.getContextParams(BaseUserInfo.class);
        //uId = "145@1234.CSZGS";
        return userIndexInfoService.findUserFfpByEmployeeUid(baseUserInfo.getUid());
    }

    @RequestMapping("/deleteUserFfpinfo")
    public JSONResult deleteUserFfpinfo(@RequestParam(name = "id") Long id) {
        boolean flag = organizationUserFfpService.delete(id);
        return JSONResult.success(flag);
    }

    /**
     * 获取我的差标
     *
     * @param userInfo
     * @return
     */
    @RequestMapping("/travelStandard")
    public JSONResult travelStandard(BaseUserInfo userInfo) {
        if (StringUtils.isBlank(userInfo.getUid())) {
            return JSONResult.errorMsg("用户为空");
        }
        if (StringUtils.isBlank(userInfo.getOrgId())) {
            return JSONResult.errorMsg("用户身份信息为空");
        }
        return JSONResult.success(userIndexInfoService.listTravelStandards(userInfo.getUid(), userInfo.getOrgId()));
    }


}